/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ReportPnl.java
 *
 * Created on May 5, 2010, 9:37:59 AM
 */
package com.aptech.campaign.ui;

import com.aptech.campaign.biz.ReportBiz;
import com.aptech.campaign.entities.Campaign;
import com.aptech.campaign.entities.Report;
import com.aptech.campaign.util.PieStatistic;
import java.util.ArrayList;
import java.util.Calendar;
import javax.swing.JOptionPane;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.data.general.PieDataset;

/**
 *
 * @author Tai Nguyen
 */
public class ReportPnl extends javax.swing.JPanel {

    private PieStatistic chartpie = null;
    ReportBiz reBuz = new ReportBiz();
    ArrayList<Campaign> arr = null;
    ArrayList<Report> arrRe = null;

    public ReportPnl() {
        initComponents();
        putDataForCbb();

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    /**
     * 
     */
    private void putDataForCbb() {
        arr = reBuz.findByAllcampaign();
        cbbCampaign.removeAllItems();
        cbbCampaign.addItem("--Select Campaign--");
        for (int i = 0; i < arr.size(); i++) {
            cbbCampaign.addItem(arr.get(i).getCampaignName());
        }
    }

    /**
     * 
     */
    private void showChartForBudget() {
        arr = reBuz.findByAllcampaign();
        int indexRow = cbbCampaign.getSelectedIndex() - 1;
        int campaignID = arr.get(indexRow).getCampaignID();
        chartpie = new PieStatistic();
        // This will create the dataset
        PieDataset dataset = chartpie.createDatasetForBudgetOfCampaign(campaignID);

        //2D cahrt
        JFreeChart chart = chartpie.createChart(dataset, "BUDGET");
        // we put the chart into a panel
        ChartPanel chartPanel = new ChartPanel(chart);
        // default size
        pnlChart.add(chartPanel);
        lblName.setText(arr.get(indexRow).getCampaignName());
        this.validate();
    }

    /**
     * 
     */
    private void showChartForSale() {
        arr = reBuz.findByAllcampaign();
        int indexRow = cbbCampaign.getSelectedIndex() - 1;
        int campaignID = arr.get(indexRow).getCampaignID();
        chartpie = new PieStatistic();
        // This will create the dataset
        PieDataset dataset = chartpie.createDatasetForSaleOfCampaign(campaignID);

        //2D cahrt
        JFreeChart chart = chartpie.createChart(dataset, "SALE");
        // we put the chart into a panel
        ChartPanel chartPanel = new ChartPanel(chart);
        // default size
        pnlChart.add(chartPanel);
        lblName.setText(arr.get(indexRow).getCampaignName());
        this.validate();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        btnGroupKind = new javax.swing.ButtonGroup();
        lblName = new javax.swing.JLabel();
        pnlChart = new javax.swing.JPanel();
        pnlShowReport = new javax.swing.JPanel();
        cbbCampaign = new javax.swing.JComboBox();
        lblCampaign = new javax.swing.JLabel();
        lblKind = new javax.swing.JLabel();
        rdbSale = new javax.swing.JRadioButton();
        rdbBudget = new javax.swing.JRadioButton();
        btnShow = new javax.swing.JButton();
        lblShow = new javax.swing.JLabel();
        dtcFirstDate = new com.toedter.calendar.JDateChooser();
        dtcSecondDate1 = new com.toedter.calendar.JDateChooser();
        lblReport = new javax.swing.JLabel();
        lbl = new javax.swing.JLabel();
        btnExport = new javax.swing.JButton();
        pnlTitle = new javax.swing.JPanel();
        lblTitle = new javax.swing.JLabel();

        setLayout(new java.awt.GridBagLayout());

        lblName.setFont(new java.awt.Font("Tahoma", 1, 15)); // NOI18N
        lblName.setText("Name Campaign");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 0.1;
        add(lblName, gridBagConstraints);

        pnlChart.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "STATISTIC", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 14), new java.awt.Color(51, 102, 255))); // NOI18N
        pnlChart.setMinimumSize(new java.awt.Dimension(200, 300));
        pnlChart.setLayout(new java.awt.BorderLayout());
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipadx = 100;
        gridBagConstraints.ipady = 100;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 0.4;
        gridBagConstraints.weighty = 0.8;
        add(pnlChart, gridBagConstraints);

        pnlShowReport.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "STATISTICS & REPORT", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 14), new java.awt.Color(51, 102, 255))); // NOI18N
        pnlShowReport.setMinimumSize(new java.awt.Dimension(200, 300));
        pnlShowReport.setPreferredSize(new java.awt.Dimension(200, 300));
        pnlShowReport.setLayout(new java.awt.GridBagLayout());

        cbbCampaign.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "--Select Campaign--" }));
        cbbCampaign.setMaximumSize(new java.awt.Dimension(200, 20));
        cbbCampaign.setMinimumSize(new java.awt.Dimension(200, 18));
        cbbCampaign.setPreferredSize(new java.awt.Dimension(200, 20));
        cbbCampaign.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbbCampaignActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.ipadx = 5;
        gridBagConstraints.ipady = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 11, 20, 0);
        pnlShowReport.add(cbbCampaign, gridBagConstraints);

        lblCampaign.setText("Campaign : ");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 20, 0);
        pnlShowReport.add(lblCampaign, gridBagConstraints);

        lblKind.setText("Report Kind : ");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 18, 0);
        pnlShowReport.add(lblKind, gridBagConstraints);

        btnGroupKind.add(rdbSale);
        rdbSale.setText("Sale");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 18, 0);
        pnlShowReport.add(rdbSale, gridBagConstraints);

        btnGroupKind.add(rdbBudget);
        rdbBudget.setSelected(true);
        rdbBudget.setText("Budget");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 11, 18, 5);
        pnlShowReport.add(rdbBudget, gridBagConstraints);

        btnShow.setBackground(new java.awt.Color(51, 102, 255));
        btnShow.setFont(new java.awt.Font("Tahoma", 1, 14));
        btnShow.setForeground(new java.awt.Color(255, 255, 255));
        btnShow.setText("Show");
        btnShow.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnShowActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 11, 15, 0);
        pnlShowReport.add(btnShow, gridBagConstraints);

        lblShow.setFont(new java.awt.Font("Tahoma", 1, 15));
        lblShow.setForeground(new java.awt.Color(51, 102, 255));
        lblShow.setText("STATISTIC");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 27, 0);
        pnlShowReport.add(lblShow, gridBagConstraints);

        dtcFirstDate.setMinimumSize(new java.awt.Dimension(150, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 10, 13, 5);
        pnlShowReport.add(dtcFirstDate, gridBagConstraints);

        dtcSecondDate1.setMinimumSize(new java.awt.Dimension(150, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 10, 13, 5);
        pnlShowReport.add(dtcSecondDate1, gridBagConstraints);

        lblReport.setFont(new java.awt.Font("Tahoma", 1, 15));
        lblReport.setForeground(new java.awt.Color(51, 102, 255));
        lblReport.setText("REPORT");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 24, 0);
        pnlShowReport.add(lblReport, gridBagConstraints);

        lbl.setText("Select Date : ");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 15, 0);
        pnlShowReport.add(lbl, gridBagConstraints);

        btnExport.setBackground(new java.awt.Color(51, 102, 255));
        btnExport.setFont(new java.awt.Font("Tahoma", 1, 14));
        btnExport.setForeground(new java.awt.Color(255, 255, 255));
        btnExport.setText("Export");
        btnExport.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExportActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 7;
        pnlShowReport.add(btnExport, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipadx = 100;
        gridBagConstraints.ipady = 100;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 0.6;
        gridBagConstraints.weighty = 0.8;
        add(pnlShowReport, gridBagConstraints);

        pnlTitle.setBackground(new java.awt.Color(51, 102, 255));
        pnlTitle.setLayout(new java.awt.BorderLayout());

        lblTitle.setBackground(new java.awt.Color(51, 102, 255));
        lblTitle.setFont(new java.awt.Font("Tahoma", 1, 30));
        lblTitle.setForeground(new java.awt.Color(51, 102, 255));
        lblTitle.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/aptech/campaign/icon/reportBanner.jpg"))); // NOI18N
        lblTitle.setMaximumSize(new java.awt.Dimension(2222, 2222));
        lblTitle.setMinimumSize(new java.awt.Dimension(252, 37));
        lblTitle.setPreferredSize(new java.awt.Dimension(300, 37));
        pnlTitle.add(lblTitle, java.awt.BorderLayout.CENTER);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 0.1;
        add(pnlTitle, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents

    private void cbbCampaignActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbbCampaignActionPerformed
    }//GEN-LAST:event_cbbCampaignActionPerformed

    private void btnShowActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnShowActionPerformed
        if (cbbCampaign.getSelectedIndex() == 0) {
            JOptionPane.showMessageDialog(this, "Please select Campaign!");
        } else {
            if (rdbBudget.isSelected()) {
                showChartForBudget();
            } else {
                showChartForSale();
            }
        }

    }//GEN-LAST:event_btnShowActionPerformed

    private void btnExportActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExportActionPerformed
        if (dtcFirstDate.getDate() != null && dtcSecondDate1.getDate() != null) {
            if (dtcFirstDate.getDate().compareTo(dtcSecondDate1.getDate()) >= 0) {
                JOptionPane.showMessageDialog(this, "StartDate invalid!");
            } else {
                //convert date calendar to date sql
                Calendar caStart = Calendar.getInstance();
                caStart.setTime(dtcFirstDate.getDate());
                java.sql.Date firstDate = new java.sql.Date(caStart.getTimeInMillis());
                Calendar caEnd = Calendar.getInstance();
                caEnd.setTime(dtcSecondDate1.getDate());
                java.sql.Date secondDate = new java.sql.Date(caEnd.getTimeInMillis());
                //
                arrRe = reBuz.findByDate(firstDate, secondDate);
                if (arrRe.size() > 0) {
                    ReportFrm re = new ReportFrm(firstDate, secondDate);
                    re.txfDate1.setText(String.valueOf(firstDate));
                    re.txfDate2.setText(String.valueOf(secondDate));
                    re.setSize(800, 500);
                    re.setVisible(true);
                } else {
                    JOptionPane.showMessageDialog(this, "Has no Campaign in this frame time!");
                }
            }
        } else {
            JOptionPane.showMessageDialog(this, "Please select Date!");
        }
    }//GEN-LAST:event_btnExportActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnExport;
    private javax.swing.ButtonGroup btnGroupKind;
    private javax.swing.JButton btnShow;
    private javax.swing.JComboBox cbbCampaign;
    private com.toedter.calendar.JDateChooser dtcFirstDate;
    private com.toedter.calendar.JDateChooser dtcSecondDate1;
    private javax.swing.JLabel lbl;
    private javax.swing.JLabel lblCampaign;
    private javax.swing.JLabel lblKind;
    private javax.swing.JLabel lblName;
    private javax.swing.JLabel lblReport;
    private javax.swing.JLabel lblShow;
    private javax.swing.JLabel lblTitle;
    private javax.swing.JPanel pnlChart;
    private javax.swing.JPanel pnlShowReport;
    private javax.swing.JPanel pnlTitle;
    private javax.swing.JRadioButton rdbBudget;
    private javax.swing.JRadioButton rdbSale;
    // End of variables declaration//GEN-END:variables
}
